<?php


########################  主题函数  ########################


function get_header(){
	include(TEMPLATEPATH.'/header.php');
}

function get_footer(){
	include(TEMPLATEPATH.'/footer.php');
}

function get_sidebar(){
	include(TEMPLATEPATH.'/sidebar.php');
}

function bloginfo($info){
	global $config;
	if(array_key_exists($info, $config['site'])){
		echo $config['site'][$info];
	}
}

function mb_theme_url($file = ''){
	global $config;
	$url = $config['site']['url'].'/mb-content/themes/'.$config['site']['theme'];
	if(!empty($file)){
		$url .= '/'.$file;
	}
	echo $url;
}

function mb_is_type($type){
	global $super;
	if($super['type'] == $type){
		return true;
	}else{
		return false;
	}
}

function mb_is_page(){
	return mb_is_type('page');
}

function mb_is_tag(){
	return mb_is_type('tag');
}

function mb_is_search(){
	return mb_is_type('search');
}

function mb_is_date(){
	return mb_is_type('date');
}

function mb_is_post(){
	return mb_is_type('post');
}

function the_title(){
	global $post;
	echo $post['post_title'];
}

function the_content(){
	global $post;
	echo $post['post_content'];
}

function the_date(){
	global $post;
	echo $post['post_date'];
}

function the_edit_url(){
	global $post;
	echo bloginfo('url').'/admin/edit-post.php?id='.$post['post_id'];
}

function the_url(){
	global $post;
	echo bloginfo('url').'/'.$post['post_id'].'.html';
}

function the_author(){
	global $post;
	echo $post['post_author'];
}

function have_next_post(){
	global $post;
	if($post['next_post'] === false){
		return false;
	}else{
		return true;
	}
}

function next_post_title(){
	global $post;
	echo $post['next_post']['post_title'];
}

function next_post_id(){
	global $post;
	echo $post['next_post']['post_id'];
}

function next_post_url(){
	global $post;
	echo bloginfo('url').'/'.$post['next_post']['post_id'].'.html';
}

function have_prev_post(){
	global $post;
	if($post['prev_post'] === false){
		return false;
	}else{
		return true;
	}
}

function prev_post_id(){
	global $post;
	echo $post['prev_post']['post_id'];
}

function prev_post_url(){
	global $post;
	echo bloginfo('url').'/'.$post['prev_post']['post_id'].'.html';
}

function prev_post_title(){
	global $post;
	echo $post['prev_post']['post_title'];
}

function mb_can_comment(){
	return false;
}

function mb_comment_code(){
	echo '<hr>';
}

function mb_page_nav(){
	global $super, $paged, $config, $uri;
	$total_page = ceil($super['posts_num'] / $config['site']['shownum']);
	if($paged > 1){
		echo '<a class="prev_page" href="'.$config['site']['url'].str_replace('-'.$paged.'.html', '-'.($paged - 1).'.html', $uri).'">前一页</a>';
	}else{
		echo '<span class="previous_page disabled">前一页</span>';
	}
	for($i=1;$i<$total_page + 1;$i++){
		if($i == $paged){
			echo '<em class="current">'.$i.'</em>';
		}else{
			if(in_array($super['type'], array('home', 'page'))){
				echo '<a href="http://mogu.cn/mogublog/page-'.$i.'.html">'.$i.'</a>';
			}elseif($super['type'] == 'cat'){
				global $cat;
				echo '<a href="http://mogu.cn/mogublog/cat-'.$cat.'-'.$i.'.html">'.$i.'</a>';
			}elseif($super['type'] == 'tag'){
				
			}
		}
	}
	if($paged < $total_page){
		echo '<a class="next_page" rel="next" href="'.$config['site']['url'].str_replace('-'.$paged.'.html', '-'.($paged + 1).'.html', $uri).'">后一页</a>';
	}else{
		echo '<span class="previous_page disabled">后一页</span>';
	}
	echo '共'.$super['posts_num'].'篇文章';
}

function have_posts(){
	if(empty($GLOBALS['posts']) || !is_array($GLOBALS['posts'])){
		return false;
	}else{
		return true;
	}
}

function the_post(){
	$GLOBALS['post'] = array_shift($GLOBALS['posts']);
}

function get_the_post_nums(){
	global $config, $super;
	if(in_array($super['type'], array('home', 'page'))){
		$result = mb_result("SELECT	COUNT(post_id) FROM ".$config['db']['prefix']."posts");
	}elseif(in_array($super['type'], array('cat', 'tag'))){
		global $cat;
		$result = mb_result("SELECT	COUNT(post_id) FROM ".$config['db']['prefix']."posts WHERE `post_cat` = '".$cat."'");
	}else{
		
	}
	
	return intval($result[0]['COUNT(post_id)']);
}

function the_posts_num(){
	global $super;
	echo $super['posts_num'];
}

function list_cats($limit = 5){
	global $config;
	$result = mb_result("SELECT	* FROM ".$config['db']['prefix']."cats limit 0,".intval($limit));
	return $result;
}

function mb_list_cats(){
	global $super, $config;
	if(is_array($super['cats']) && !empty($super['cats'])){
		foreach($super['cats'] as $v){
			echo '<a href="'.$config['site']['url'].'/cat-'.$v['cat_slug'].'.html">'.$v['cat_name'].'<span>'.$v['count'].'</span></a>';
		}
	}
}


########################  数据库函数  ########################


function mb_connect_db(){
	global $config;
	$con = mysql_connect($config['db']['host'].':'.$config['db']['port'], $config['db']['user'], $config['db']['pwd']) or die(mysql_error());
	mysql_set_charset($config['db']['char'], $con);
	mysql_select_db($config['db']['name']);
	if($con){
		$GLOBALS['db'] = $con;
	}else{
		$GLOBALS['db'] = false;
	}
}

function mb_query($sql){
	global $db;
	if($db){
		return mysql_query($sql);
	}else{
		return false;
	}
}

function mb_result($sql){
	$query = mb_query($sql);
	if($query){
		$result = array();
		while($res = mysql_fetch_assoc($query)){
			$result[] = $res;
		}
		return $result;
	}else{
		return false;
	}
}

function mb_close_db(){
	global $db;
	if($db){
		mysql_close($db);
	}
}


########################  文章函数  ########################


function mb_insert_post($post = ''){
	global $db, $config;
	if(!$db){
		return false;
	}
	$post = array(
		'post_title'    => 'My post',
		'post_content'  => 'This is my post.',
		'post_status'   => 'publish',
		'post_author'   => 1,
		'post_cat' => 1
	);
	$post_title = htmlspecialchars($post['post_title']);
	$post_content = htmlspecialchars($post['post_content']);
	if(in_array($post['post_status'], array('publish', 'draft', 'shenhe'))){
		$post_status = $post['post_status'];
	}else{
		$post_status = 'draft';
	}
	$post_author = intval($post['post_author']);
	$post_cat = intval($post['post_cat']);
	$post_date = date('Y-m-d H:i:s');
	$sql = "INSERT INTO `".$config['db']['prefix']."posts` (`post_author`, `post_date`, `post_content`, `post_title`, `post_status`, `post_cat`) VALUES (".$post_author.", '".$post_date."', '".$post_content."', '".$post_title."', '".$post_status."', '".$post_cat."')";
	$query = mb_query($sql);
	if($query){
		return mysql_insert_id();
	}else{
		return false;
	}
}

function get_post($id){
	global $config;
	$id = intval($id);
	$posts = mb_result("SELECT * FROM `".$config['db']['prefix']."posts` WHERE `post_id` = '".$id."'");
	if(empty($posts)){
		return array();
	}else{
		$posts[0]['next_post'] = get_nearby_post($id, 'next');
		$posts[0]['prev_post'] = get_nearby_post($id, 'prev');
		return $posts[0];
	}
}

function get_nearby_post($id, $type){
	global $config;
	$id = intval($id);
	if($type == 'prev'){
		$posts = mb_result("SELECT post_id,post_title FROM `".$config['db']['prefix']."posts` WHERE `post_id` < '".$id."' ORDER BY `post_id` DESC LIMIT 0,1");
	}else{
		$posts = mb_result("SELECT post_id,post_title FROM `".$config['db']['prefix']."posts` WHERE `post_id` > '".$id."' LIMIT 0,1");
	}
	if(empty($posts)){
		return false;
	}else{
		return $posts[0];
	}
}

function get_posts($type, $paged = ''){
	global $config;
	$paged = empty($paged) ? 1 : intval($paged);
	$limit = ( $paged - 1 ) * $config['site']['shownum'];
	if($type == 'home'){
		$posts = mb_result("SELECT post_id,post_title,post_author,post_date,post_content,post_cat FROM `".$config['db']['prefix']."posts` ORDER BY `post_date` DESC LIMIT 0,".intval($config['site']['shownum']));
	}elseif($type == 'page'){
		$posts = mb_result("SELECT post_id,post_title,post_author,post_date,post_content,post_cat FROM `".$config['db']['prefix']."posts` ORDER BY `post_date` DESC LIMIT ".$limit.",".intval($config['site']['shownum']));
	}elseif($type == 'cat'){
		global $cat;
		// var_dump($cat);
		$posts = mb_result("SELECT post_id,post_title,post_author,post_date,post_content,post_cat FROM `".$config['db']['prefix']."posts` WHERE `post_cat` = '".$cat."' ORDER BY `post_date` DESC LIMIT ".$limit.",".intval($config['site']['shownum']));
	}else{
		$posts = array();
	}
	return $posts;
}


